/**
 * @constructor
 * @param {Number} mowerIndex
 * @param {MowersDrivingContext} drivingContext
 * @returns {Mower}
 */
function Mower(mowerIndex, drivingContext) {
	this.mowerIndex = mowerIndex;
	this.drivingContext = drivingContext;
	this.currentVector = null;
	this.commandCounter = 0;
};

var DRIVING_HELPER = new LawnDrivingHelper();

var DIRECTION = {
		N:{
			name: "N",
			xAdvancement:0,
			yAdvancement:1,
			degreesFromNorthCW:0
		},
		S:{
			name: "S",
			xAdvancement:0,
			yAdvancement:-1,
			degreesFromNorthCW:180
		},
		E:{
			name: "E",
			xAdvancement:1,
			yAdvancement:0,
			degreesFromNorthCW:90
		},
		W:{
			name: "W",
			xAdvancement:-1,
			yAdvancement:0,
			degreesFromNorthCW:270
		}
};

var COMMAND = {
		D: {name:"D", rotationDegreesDeltaCW:90},
		G: {name:"G", rotationDegreesDeltaCW:-90},
		A: {name:"A", rotationDegreesDeltaCW:0},
};

/**
 * @public
 * @throws {PositionOutsideLawnException}
 * @param {MowerVector} startVector
 */
Mower.prototype.placeMower = function(startVector) {
	if(DRIVING_HELPER.isOutsideLawn(startVector.position, this.drivingContext)) {
		throw new PositionOutsideLawnException("Invalid position, mower is outisde lawn!");
	}
	this.currentVector = startVector;
};

/**
 * @public
 * @param {COMMAND} command
 * @returns {MowerVector}
 */
Mower.prototype.execute = function(command) {
	this.commandCounter++;
	var newMowerVector = null;
	if(COMMAND.A == command) {
		newMowerVector = this.currentVector.advance();		
		if(DRIVING_HELPER.isOutsideLawn(newMowerVector.position, this.drivingContext)) {
			throw new PositionOutsideLawnException("Mower #" +  this.mowerIndex + ": Command #" +  this.commandCounter + " ignored, mower would exit lawn!");
		}
	} else {
		newMowerVector = this.currentVector.rotate(command);			
	}
	this.currentVector = newMowerVector;		
	return this.currentVector;
};

/*
 * @public
 * @returns {Number}
 */
Mower.prototype.getMowerIndex = function() {
	return this.mowerIndex;
};

